/**
 * Created by Tim on 2017/5/23.
 */
$(function () {

    var platformId = $('#platformId');

    function PropertyMgr() {
        // 表格ID
        this.dataTable = $("#dataTable");
        // 按钮
        this.$seachr_btn = $('#btn_search');
        this.$btn_search_clear = $('#btn_search_clear');

        // 初始化
        this.init();
    }

    PropertyMgr.prototype.Constructor = PropertyMgr;

    PropertyMgr.prototype.init = function () {
        this.loadTable();
        this.registEvent();
    };

    PropertyMgr.prototype.loadTable = function () {
        this.dataTable.dataTable({
            "sAjaxSource": "/user_property_dossier/page",
            "aoColumns": [
                {"sWidth": "8%", "sTitle": "姓名", "sClass": "center", "mData": "name", "bSortable": false},
                {"sWidth": "8%", "sTitle": "电话", "sClass": "center", "mData": "phone", "bSortable": false},
                {"sWidth": "8%", "sTitle": "性别", "sClass": "center", "mData": "sex", "bSortable": false},
                {"sWidth": "8%", "sTitle": "详细地址", "sClass": "center", "mData": "address", "bSortable": false},
                {"sWidth": "8%", "sTitle": "登记时间", "sClass": "center", "mData": "created", "bSortable": false},
                {"sWidth": "8%", "sTitle": "状态", "sClass": "center", "mData": "status", "bSortable": false},
                {"sWidth": "16%", "sTitle": "操作", "sClass": "center", "mData": "id", "bSortable": false}
            ],
            "aLengthMenu": [10, 30, 50],
            "aaSorting": [],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                var id = aData["id"];
                if (aData['sex']) {
                    $('td:eq(2)', nRow).html(aData['sex']==1?'男':'女');
                }

                if (aData['created']) {
                    $('td:eq(4)', nRow).html(SysTool.Date.formatDate(aData['created'], 'yyyy-MM-dd hh:mm:ss'));
                }
                status = aData['status'];
                if (status==0) {
                    $('td:eq(5)', nRow).html('<span class="label label-primary arrowed">待审核</span>');
                } else if(status ==1){
                    $('td:eq(5)', nRow).html('<span class="label label-success arrowed">已通过</span>');
                } else
                    $('td:eq(5)', nRow).html('<span class="label label-danger arrowed">未通过</span>');
                //操作
                var option = '<a title="修改商场信息" href="/user_property_dossier/' + id + '" class="btn btn-xs btn-primary">详情</a>';
                // option += '&nbsp;<a title="查询下单用户档案信息" href="' + "" + '/market/account/' + id + '" class="btn btn-xs btn-primary">收款设置</a>';


                $('td:eq(6)', nRow).html(option);
            },
            "fnServerParams": function (aoData) {
                queryCondition("#search-form", aoData);
            },
            "fnStateLoaded": function (oSettings, oData) {
//                alert(JSON.stringify(oData));
            }
        });

        function queryCondition(queryFormId, aoData) {
            var queryParameters = $(queryFormId).serializeArray();
            $(queryParameters).each(function (i, v) {
                aoData.push(v);
            });
        }
    };

    PropertyMgr.prototype.refreshTable = function () {
        this.dataTable.dataTable().fnClearTable();
    };

    PropertyMgr.prototype.registEvent = function () {
        var that = this;
        that.$seachr_btn.click(function () {
            var oSettings = that.dataTable.dataTable().fnSettings();
            oSettings._iDisplayStart = 0;
            that.dataTable.dataTable().fnClearTable();
        });

        that.$btn_search_clear.click(function () {
            $('#carNo').val('');
            var str = '<option value="-1">--请选择--</option>' +
                '<option value="0">待审核</option>' +
                '<option value="1">已通过</option>';
            $('#status').val(-1).html(str);
            that.$seachr_btn.click();
        });

        $('.date-picker').datepicker({autoclose: true}).next().on(ace.click_event, function () {
            //$(this).prev().focus();
        });

        SysTool.Html.bindEnterClick([], that.$seachr_btn);
    };

    window.PropertyMgr = new PropertyMgr();
});